package com.qidian.by.goods.controller;

import com.qidian.by.goods.service.IItemService;
import com.qidian.by.goods.vo.ItemAndSpeRelationVO;
import com.qidian.by.goods.vo.ItemVO;
import com.qidian.by.pojo.goods.entity.Item;
import com.qidian.by.util.pojo.EsResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;


/**
 * <p>
 * 商品表 前端控制器
 * </p>
 *
 * @author 小可怎么都睡不够
 * @since 2024-05-08
 */
@RestController
@RequestMapping("/item")
public class ItemController {
    @Autowired
    private IItemService itemService;

    @GetMapping("/querySpeIdByItemId")
    public EsResult querySpeIdByItemId(Long itemId){
        return itemService.getSpeIdByItemId(itemId);
    }
    @PostMapping("/addItemAndSpeRelation")
    public EsResult addItemAndSpeRelation(@RequestBody ItemAndSpeRelationVO vo){
        List<Long> longRes = new ArrayList<>();
        vo.getSpeId().forEach(speId -> {
            longRes.add(Long.valueOf(vo.getItemId()));
        });
        return itemService.addItemAndSpeRelation(Long.valueOf(vo.getItemId()),longRes);
    }
    @GetMapping("/list")
    public EsResult list(Item item, @RequestParam(defaultValue = "1")int page, @RequestParam(defaultValue = "10")int limit){
        return itemService.list(item,page,limit);
    }

    @GetMapping("/get")
    public EsResult getItemById(Long itemId){
        return itemService.getItemById(itemId);
    }

    @PostMapping("/save")
    public EsResult save(@RequestBody ItemVO vo){
        return itemService.save(vo);
    }

    @GetMapping("/getItemById")
    public EsResult getItemInfoById(Long itemId){
        return itemService.getItemInfoById(itemId);
    }
}
